/*
 * SnmpOidApi.cpp
 *
 *  Created on: 2016-1-12
 *      Author: root
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include"SnmpOidApi.h"
#include"PublicLog.h"
void OidAddNum(SnmpOidInfo&oidinfo,long num){
    oidinfo.oidName[oidinfo.oidLen++]=num;
}
void OidAddIP(SnmpOidInfo&oidinfo,const char*IP){
    sscanf(IP,"%lu.%lu.%lu.%lu",
            &oidinfo.oidName[oidinfo.oidLen+0],
            &oidinfo.oidName[oidinfo.oidLen+1],
            &oidinfo.oidName[oidinfo.oidLen+2],
            &oidinfo.oidName[oidinfo.oidLen+3]);
    oidinfo.oidLen+=4;
}
#if 0
void OidAddMac(SnmpOidInfo&oidinfo,const char*Mac){
    sscanf(Mac,"%02lX:%02lX:%02lX:%02lX:%02lX:%02lX",
                &oidinfo.oidName[oidinfo.oidLen+0],
                &oidinfo.oidName[oidinfo.oidLen+1],
                &oidinfo.oidName[oidinfo.oidLen+2],
                &oidinfo.oidName[oidinfo.oidLen+3],
                &oidinfo.oidName[oidinfo.oidLen+4],
                &oidinfo.oidName[oidinfo.oidLen+5]);
    oidinfo.oidLen+=6;
}

//OID读取IP
void OidReadIP(SnmpOidInfo&oidinfo,int Pos,char*IP){
    sprintf(IP,"%lu.%lu.%lu.%lu",
                oidinfo.oidName[Pos+0],
                oidinfo.oidName[Pos+1],
                oidinfo.oidName[Pos+2],
                oidinfo.oidName[Pos+3]);
}
//OID读取Mac
void OidReadMac(SnmpOidInfo&oidinfo,int Pos,char*Mac){
    sprintf(Mac,"%02lX.%02lX.%02lX.%02lX.%02lX.%02lX",
               oidinfo.oidName[Pos+0],
               oidinfo.oidName[Pos+1],
               oidinfo.oidName[Pos+2],
               oidinfo.oidName[Pos+3],
               oidinfo.oidName[Pos+4],
               oidinfo.oidName[Pos+5]);
}
#endif
//OID读取IP
void OidReadIP(oid*name,int Pos,char*IP){
    sprintf(IP,"%lu.%lu.%lu.%lu",
            name[Pos+0],name[Pos+1],
            name[Pos+2],name[Pos+3]);
}
//OID读取IP
#include<arpa/inet.h>
void OidReadIPV6(oid*name,int Pos,char*IPV6){
	struct in6_addr sin6_addr;
	int i=0;
	while(i<16){
		sin6_addr.s6_addr[i]=name[Pos+i];
		i++;
	}
	inet_ntop(AF_INET6,&sin6_addr,IPV6,40);
}
//OID读取Mac
void OidReadMac(oid*name,int Pos,char*Mac){
    sprintf(Mac,"%02lX:%02lX:%02lX:%02lX:%02lX:%02lX",
            name[Pos+0],name[Pos+1],name[Pos+2],
            name[Pos+3],name[Pos+4],name[Pos+5]);
}

#if 0
void OidAddMac(oid*name,size_t*len,const char*strMAC){
    u_char temp[6];
    sscanf(strMAC,"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
                &temp[0],&temp[1],&temp[2],&temp[3],&temp[4],&temp[5]);
    name[(*len)++]=temp[0];
    name[(*len)++]=temp[1];
    name[(*len)++]=temp[2];
    name[(*len)++]=temp[3];
    name[(*len)++]=temp[4];
    name[(*len)++]=temp[5];
}
void OidAddIP(oid*name,size_t*len,const char*strIP){
    u_char temp[4];
    sscanf(strIP,"%hhu.%hhu.%hhu.%hhu",&temp[0],&temp[1],&temp[2],&temp[3]);
    name[(*len)++]=temp[0];
    name[(*len)++]=temp[1];
    name[(*len)++]=temp[2];
    name[(*len)++]=temp[3];
}
void OidAddIndex(oid*name,size_t*len,long port){
    name[(*len)++]=port;
}
#endif

long CheckCiscoVLAN(netsnmp_variable_list*vars,const char*IP,const char*Descr){
    long VlanIfIndex=*vars->val.integer;
    long CheckIfIndex=vars->name[vars->name_length-1];
    if((VlanIfIndex==0)||(VlanIfIndex==1)||
       ((CheckIfIndex!=VlanIfIndex)&&(CheckIfIndex>1000))){
       DEBUG("思科特殊处理%s:%s跳过 %ld %ld",Descr,IP,CheckIfIndex,VlanIfIndex);
       return 0;
     }
    return CheckIfIndex;
}
